package com.codeTop;

/**
 * @Author: shaki
 * @Date: 2021/8/18 15:55
 */
public class 最大子序和 {
    public static void main(String[] args) {
        int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
        int sum = maxSubArray(nums);
        System.out.println(sum);
    }
    public static int maxSubArray(int[] nums) {
        if(nums.length == 1){
            return nums[0];
        }
        int max = Integer.MIN_VALUE;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            count += nums[i];
            max = Math.max(max, count);
            if(count <= 0){
                count = 0;
            }
        }
        return max;
    }
}
